package com.library.guard.controller;

import ZO_API.NO1_Api;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.guard.core.domain.AntSet;
import com.library.guard.core.domain.DeviceReport;
import com.library.guard.core.domain.GpoSet;
import com.library.guard.core.service.ZeroAndOneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 书籍副本Controller
 *
 * @author hb
 * @date 2024-10-14
 */
@Api(tags = "智能门禁")
@RestController
@RequestMapping("/books/guard")
public class GuardController extends BaseController
{
    @Resource
    ZeroAndOneService zeroAndOneService;

    /**
     * 设置GPO
     */
    @PreAuthorize("@ss.hasRole('admin')")
    @ApiOperation("设置GPO")
    @PutMapping("/gpo")
    public AjaxResult setGpo(@RequestBody GpoSet gpoSet) {
        return toAjax(zeroAndOneService.setGpo(gpoSet));
    }

    /**
     * 设置ANT
     */
    @PreAuthorize("@ss.hasRole('admin')")
    @ApiOperation("设置ANT")
    @PutMapping("/ant")
    public AjaxResult setAnt(@Validated @RequestBody AntSet antSet) {
        return toAjax(zeroAndOneService.setAnt(antSet));
    }

    @ApiOperation("设置读到标签后GPO")
    @PostMapping("/check1")
    public AjaxResult check1(@RequestBody DeviceReport deviceReport , HttpServletRequest request) throws IOException {
        logger.info("ip[{}] submit json data [{}]", request.getRemoteHost() ,StreamUtils.copyToString(request.getInputStream(),Charset.defaultCharset()));
        if (! "20001".equals(deviceReport.getCmd())) {
            return success();
        }
        deviceReport.setIp(request.getRemoteHost());
//        zeroAndOneService.checkBooks(deviceReport);
        zeroAndOneService.checkBooksLocalTest(deviceReport);
        return success();
    }

    @ApiOperation("设置读到标签后GPO")
    @PostMapping("/check2")
    public AjaxResult check2(@RequestBody DeviceReport deviceReport , HttpServletRequest request) throws IOException {
        logger.info("ip[{}] submit json data [{}]", request.getRemoteHost() ,StreamUtils.copyToString(request.getInputStream(),Charset.defaultCharset()));
        if (! "20001".equals(deviceReport.getCmd())) {
            return success();
        }
        deviceReport.setIp(request.getRemoteHost());
        zeroAndOneService.checkBooks(deviceReport);
        return success();
    }

    @GetMapping("/{led}")
    public AjaxResult greed(@PathVariable String led)  {
        zeroAndOneService.led("172.20.12.33",led);
        return success();
    }

    /** 模拟读卡器启动 */
    @GetMapping("/demo")
    public AjaxResult demo()  {
        zeroAndOneService.demo();
        return success();
    }

}
